package com.qiu.greened.utils

import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatter
import java.util.*

private const val minute = (60 * 1000).toLong() // 1分钟
private const val hour = 60 * minute // 1小时
private const val day = 24 * hour // 1天
private const val month = 31 * day // 月
private const val year = 12 * month // 年


fun getTimeFormatText(time: String): String? {
    var sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
    val date = sdf.parse(time) ?: return null

    val diff: Long = Date().time - date.time
    var r: Long = 0
    if (diff > year) {
        val formatter = SimpleDateFormat("yyyy-MM-dd")
//        val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        val dateString: String = formatter.format(date)
        return dateString
    }
    if (diff > day && diff < year) {
        r = diff / day
        val formatter = SimpleDateFormat("MM-dd")
        val dateString: String = formatter.format(date)
        return dateString
    }
    if (diff > hour && diff < day) {
        r = diff / hour
        return r.toString() + "小时前"
    }
    if (diff > minute && diff < hour) {
        r = diff / minute
        return r.toString() + "分钟前"
    }
    return "刚刚"
}